package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;

import javax.swing.table.DefaultTableModel;

import implementor.CampaignImp;
import daoInterfaces.CampaignDao;
import model.CampaigMgmtModel;
import utility.ViewUtilites;
import view.campaign.CampaignHome;

public class CampaignReportController implements ActionListener
{
	CampaignHome view;
	CampaigMgmtModel model;
	
	
	public CampaignReportController() 
	{
		
	}


	public CampaignReportController(CampaignHome view, CampaigMgmtModel model) {
		super();
		this.view = view;
		this.model = model;
		view.actionListener(this);
	}


	public CampaignHome getView() {
		return view;
	}


	public void setView(CampaignHome view) {
		this.view = view;
	}


	public CampaigMgmtModel getModel() {
		return model;
	}


	public void setModel(CampaigMgmtModel model) {
		this.model = model;
	}
	
	public void reactOnApplyFilter()
	{
		String start=view.gettStartDate().getText();
		java.sql.Date startDate=ViewUtilites.convert(start);
		String end=view.gettEndDate().getText();
		java.sql.Date endDate=ViewUtilites.convert(end);
		String created=view.getComboCreatedBy().getSelectedItem().toString();
		CampaignDao imp=new CampaignImp();
		CampaigMgmtModel[] m=imp.retriveOnMultipleCondition(startDate, endDate, created, 1+"");
		
		
		DefaultTableModel dm=(DefaultTableModel) view.getTable().getModel();
		for(int i=0;i<m.length;i++)
		{
			dm.addRow(new Object[]{m[i].getCampaignName(),m[i].getCreatedBy(),m[i].getStartDate(),m[i].getEndDate(),m[i].getDescription()});
		}
	}


	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String k=e.getActionCommand();
		if(k.contains("Apply Filter"))
		{
			reactOnApplyFilter();
		}
		if(k.contains("Cl"))
		{
			ViewUtilites.makeDateComponent(view.gettStartDate());
		}
		if(k.contains("Cl"))
		{
			ViewUtilites.makeDateComponent(view.gettEndDate());
		}
	}

}
